# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 10, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
# SUBSET: ONLY NON-HISPANIC WHITE RESPONDENTS. 
our.data = subset(our.data, whiteNH==1)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# AID TO BLACKS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~aidblacks, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1 & whiteNH==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~aidblacks, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1 & whiteNH==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.aidblacks = subset(means.data, aidblacks!=0)
means.data.aidblacks$item = rep("Government \naid to Blacks", 
                               length(means.data.aidblacks$year))
names(means.data.aidblacks)[3] = "average"
means.data.aidblacks = means.data.aidblacks[-c(4)]
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# AFFIRMATIVE ACTION

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~affirmativeaction, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1 & whiteNH==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~affirmativeaction, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1 & whiteNH==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.affirmativeaction = subset(means.data, affirmativeaction!=0)
means.data.affirmativeaction$item = rep("Affirmative action \nin hiring", 
                                        length(means.data.affirmativeaction$year))
names(means.data.affirmativeaction)[3] = "average"
means.data.affirmativeaction = means.data.affirmativeaction[-c(4)]
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# RACIAL RESENTMENT

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)
# POSITION, REPUBLICANS
our.means.republicans = svyby(~rr_total, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1 & whiteNH==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS
our.means.democrats = svyby(~rr_total, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1 & whiteNH==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# REMOVE EMPTY YEARS; CLEAN UP
means.data.rr_total = subset(means.data, rr_total!=0)
means.data.rr_total$item = rep("Racial resentment \n(index of four items)", 
                               length(means.data.rr_total$year))
names(means.data.rr_total)[3] = "average"
means.data.rr_total = means.data.rr_total[-c(4)]
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE 
new.data = rbind(means.data.aidblacks, 
                 means.data.affirmativeaction, 
                 means.data.rr_total)
new.data

# FIGURE 10: WHITES' ATTITUDES ON RACIAL POLITICS, 1980-2020, BY 
# PARTISANSHIP AND MATCHING STATUS. 
our.plot = ggplot(new.data,
                  aes(year, average, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") + 
  xlab("Year") +
  ylab("Average \n") +
  # ggtitle("Whites' Attitudes on Racial Politics, 1980-2020, \nBy Partisanship and Matching Status", 
  #        subtitle = "Data: ANES cumulative file, White partisan identifiers only. Higher scores indicate more \nconservative attitudes. All variables rescaled between 0-1.") + 
  theme_minimal() +  
  # scale_color_grey() +
  ylab("Average \n") + 
  facet_grid(programmatic ~ item, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  theme(axis.text = element_text(size = 9)) + 
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 9)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 9)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + 
  # geom_hline(yintercept = 0.5, linetype = "dashed") + 
  ylim(0, 1) +
  theme(panel.spacing.x = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure10.pdf", width = 6.6, height = 9)

